package me.haui.plg.backingBean.print;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import me.haui.plg.entity.Leitfaden; /*
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
*/

/**
 *
 * @author haui
 */
@ManagedBean(name="printController")
@RequestScoped
public class PrintController {
    
    /**
    @PersistenceContext(unitName = "me.hausegger_ProspectLeitfadenGenerator_war_1.0-SNAPSHOTPU")
    private EntityManager em;            
    **/
    

    
    @ManagedProperty(value="#{param.leitfadenName}")
    private String leitfadenName;

    
   
    
    public String getLeitfadenName() {
        return leitfadenName;
    }

    public void setLeitfadenName(String leitfadenName) {
        this.leitfadenName = leitfadenName;
    }
    
    
    
    public void printLeitfaden(Leitfaden  currentLeitfaden) throws NamingException, SQLException, IOException/*, JRException*/{

        
          PrintLeitfadenCompletelyManually printReport = new PrintLeitfadenCompletelyManually();
          printReport.doPrint(currentLeitfaden);

        
        //http://www.ramkitech.com/2011/11/jsf-jpa-jasperreports-ireport.html
        //http://java.dzone.com/articles/java-reporting-part-2


        //System.out.println("Hallo!!!!");
        
        //http://community.jaspersoft.com/questions/536539/noclassdeffounderror-jrstyledtextparser-initialization-failure
        //http://stackoverflow.com/questions/3628302/jasperreports-noclassdeffounderror-exception-on-net-sf-jasperreports-engine-util
        //Funktioniert nicht
        //System.setProperty("java.awt.headless", "true");
/*
        JasperPrint jasperPrint;
        ServletOutputStream servletOutputStream = null;
        
        Map<String, Object> parameterHashMap = new HashMap<String, Object>();        
        
        parameterHashMap.put("LeitfadenNr", currentLeitfaden.getId());


        //System.out.println( this.em.getEntityManagerFactory().getProperties().get("javax.persistence.jtaDataSource") );

        
        Context ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("ProspectLeitfadenGeneratorDB");        
        Connection conn = ds.getConnection();
        
        
        
        String  reportPath=  FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report/Leitfaden_neu.jasper");        

        
        jasperPrint = JasperFillManager.fillReport(reportPath, parameterHashMap,conn);          
        
                

        HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();          
        
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + currentLeitfaden.getName().replace(" ", "_") +"_Leitfaden.docx");  
        //Problem von Firefox dass ausschließlich Content-disposition nicht reicht um
        //downgeloadete Datei mit Microsoft Word zu öffnen. Datei wird zB direkt von Firefox
        //Browser geöffnet. Was XML Parserfehler zur Folge hat.
        httpServletResponse.addHeader("Content-Type", "application/msword");  
        
        servletOutputStream = httpServletResponse.getOutputStream();  
        JRDocxExporter docxExporter=new JRDocxExporter();  
        
        docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
        docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);  
        
        docxExporter.exportReport();  
        FacesContext.getCurrentInstance().responseComplete();         
*/        

        
    }

            
}
